<?php
namespace Home\Controller;

use Think\Controller;
use Think\Model;

class GoodsController extends Controller {

	public function goodsattrAction($id=0,$info=array())
	{	
		$goods = M('goods');
		$ginfo = $goods->where('pid='.$id)->select();
		$num = count($ginfo);
		for($i=0;$i<$num;$i++) {
			$len = strlen($ginfo[$i]['path']);
			$name= str_pad($ginfo[$i]['attr'],$len,'>',STR_PAD_LEFT );
			$info[] = $name;
			$this->goodsattrAction($ginfo[$i]['id'],$info);
		}

			
	
		// var_dump($info);exit;

		$this->assign('info',$info);
		$this->display();
		
	}

	public function foodAction()
	{	
		if(IS_GET)
		{
			$foods = M('foods');
			$data = $foods->where("status=0")->select();
			$this->assign('data',$data);
			$this->display();
			return;
		}
		$status = $_POST['status'];

		$foods = M('foods');
		$data = $foods->where("status='$status'")->select();
		if($data)
		{
			$this->ajaxReturn($data);
		}

		
	}

	public function addfoodAction()
	{
		// if(!IS_POST)
		// {
		// 	$res['status'] = 0;
		// 	$res['content'] = '请求方式不正确!';
		// 	$this->ajaxReturn($res);
		// }

		$name = $_POST['name'];
		// var_dump($name);exit;
		$food = M('foods');
		$res = $food->where("name='$name'")->select();
		if($res)
		{
			$res['status'] = 0;
			$res['content'] = '物品已存在!';
			$this->ajaxReturn($res);
		}
		$data['name'] = $name;
		$result = $food->add($data);

		if(!$result)
		{
			$res['status'] = 0;
			$res['content'] = '添加失败!';
			$this->ajaxReturn($res);
		}else
		{
			$res['status'] = 1;
			$res['content'] = '添加成功!';
			$this->ajaxReturn($res);
		}
	}


}